const superagent = require('superagent');
const cheerio = require('cheerio');
const Nightmare = require('nightmare');
const nightmare = Nightmare({show: true});
const url = 'https://movie.douban.com/explore#!type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start=0';


// 无法抓取后请求的数据
superagent.get(url).end((err, res) => {
    if (err) {
        console.log('抓取失败');
    } else {

    }
});
const sleep = time => new Promise(resolve => {
    setTimeout(resolve, time)
}).catch(err => {
    console.log(err);
});
// 模拟用户操作，可以拿到请求之后的数据
;(async () => {
    await nightmare.goto(url);
    await nightmare.wait('.list-wp .more');
    for(let i = 0; i < 2; i++){
        await sleep(3000);
        await nightmare.click('.list-wp .more');
    }
    await nightmare.wait(() => {
            const text = document.querySelector('.list-wp .more').innerText;
            if (text === '加载更多') {
                return true
            }
        })
        .evaluate(() => document.querySelector('.list-wp').innerHTML)
        .then((htmStr) => {
            const result = getMovies(htmStr);
            process.send({result});
            process.exit(0);
        });
})();
/*nightmare
    .goto(url)
    .wait('.list-wp .list a')
    .evaluate(() => document.querySelector('.list-wp').innerHTML)
    .then((htmStr) => {
        movies = getMovies(htmStr);
        console.log(movies)
    });*/
// 拿到页面进行处理，把需要的数据拿出来
const getMovies = (res) => {
    let links = [];
    let $ = cheerio.load(res);
    $('.list a.item').each((index, item) => {
        const it = $(item);
        const doubanId = it.find('div').data('id');
        //const title = it.find('p').text().replace(/[\s\r\n]/g,"");
        const title = it.find('img').attr('alt');
        const rate = Number(it.find('strong').text());
        const poster = it.find('img').attr('src').replace('s_ratio', 'l_ratio');
        links.push({
            doubanId,
            title,
            rate,
            poster,
        });
    });
    return links;
};
